﻿@{ Layout = "~/Views/Shared/_ModalForm.cshtml"; }

<div class="wrapper animated fadeInRight">
    <form id="form" class="form-horizontal m">
        <div class="form-group">
            <label class="col-sm-2 control-label ">账号<font class="red"> *</font></label>
            <div class="col-sm-4">
                <input id="account" col="Account" type="text" class="form-control" />
            </div>
            <label class="col-sm-2 control-label ">密码<font class="red"> *</font></label>
            <div class="col-sm-4">
                <input id="password" col="Password" type="password" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">姓名<font class="red"> *</font></label>
            <div class="col-sm-4">
                <input id="name" col="Name" type="text" class="form-control" />
            </div>
            <label class="col-sm-2 control-label ">性别</label>
            <div class="col-sm-4" id="gender" col="Gender"></div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">生日</label>
            <div class="col-sm-4">
                <input id="birthday" col="Birthday" type="text" class="form-control" />
            </div>
            <label class="col-sm-2 control-label">状态</label>
            <div class="col-sm-4" id="status" col="Status"></div>
            
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">邮箱</label>
            <div class="col-sm-4">
                <input id="email" col="Email" type="text" class="form-control" />
            </div>
            <label class="col-sm-2 control-label ">手机</label>
            <div class="col-sm-4">
                <input id="mobile" col="Mobile" type="text" class="form-control" />
            </div>
           
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">微信</label>
            <div class="col-sm-4">
                <input id="weChat" col="WeChat" type="text" class="form-control" />
            </div>
            <label class="col-sm-2 control-label ">QQ</label>
            <div class="col-sm-4">
                <input id="qq" col="Qq" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">组织</label>
            <div class="col-sm-10">
                <div id="masterId" col="MasterId"></div>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-2 control-label ">描述</label>
            <div class="col-sm-10">
                <textarea id="description" col="Description" class="form-control" style="height:68px"></textarea>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    var id = ys.request("id");
    var act = 'edit';
    if (id == '' | id == null) act = 'add';
    var masterId = ys.request("masterId");

    $(function () {
        $("#status").ysRadioBox({ data: ys.getJson(@Html.Raw(typeof(StatusType).EnumToDictionaryJson())) });
        $("#gender").ysRadioBox({ data: ys.getJson(@Html.Raw(typeof(GenderType).EnumToDictionaryJson())) });
        $('#masterId').ysComboBoxTree({ url: '@Url.Content("~/Sys/User/GetOrganizationTreeJsonForSelectMaster")' });
        laydate.render({ elem: '#birthday', format: 'yyyy-MM-dd' });

        getForm();

        $("#form").validate({
            rules: {
                account: { required: true, maxlength: 31},
                name: { required: true, maxlength: 63},
                //masterId: { required: true },//no use
                password: {
                    required: true,
                    minlength: 6,
                    maxlength: 20
                },
                mobile: { isPhone: true, maxlength: 15},
                email: { email: true, maxlength: 63 },
                //qq: { email: true, maxlength: 16 },
                wechat: {maxlength: 31 },
            }
        });
    });

    function getForm() {
        if (act =='add') {//add
            var defaultData = {};
            defaultData.Account = "";
            defaultData.Password = "";
            defaultData.Gender = "@((int)GenderType.Unknown)";
            defaultData.Status = "@((int)StatusType.Enabled)";
            defaultData.MasterId = masterId;
            if (!masterId) {
                $("#masterId_input").val("请选择组织");
            }

            $("#form").setWebControls(defaultData);
        }
        else {//edit
            $('#password').attr("readonly", "readonly").attr("disabled", "disabled");
            ys.ajax({
                url: '@Url.Content("~/Sys/User/GetEditDtoJson")' + '?id=' + id,
                type: "get",
                success: function (rst) {
                    if (rst.Flag == 1) {
                        var result = rst.Data;
                        $("#form").setWebControls(result);
                    }
                    else ys.msgError(rst.Message);
                }
            });
        }
    }

    function saveForm(index) {
        if(!ys.checkTextLenth("description", "描述", 0, 127)) return;
        if (!ys.checkSelectedValue("masterId_input", "组织", "请选择组织")) return;

        if ($("#form").validate().form()) {
            var postData = $("#form").getWebControls({ Id: id });
            postData.MasterId = ys.getLastValue(postData.MasterId);
            ys.ajax({
                url: '@Url.Content("~/Sys/User/")' + act,
                type: "post",
                data: postData,
                success: function (rst) {
                    if (rst.Flag == 1) {
                        ys.msgSuccess(rst.Message);
                        parent.searchGrid();
                        parent.layer.close(index);
                    }
                    else {
                        ys.msgError(rst.Message);
                    }
                }
            });
        }
    }
</script>
